package com.clkj.goldpartner.controller;

import com.clkj.goldpartner.pojo.*;
import com.clkj.goldpartner.pojo.common.DateFormatHelper;
import com.clkj.goldpartner.pojo.common.Result;
import com.clkj.goldpartner.pojo.common.StatusCode;
import com.clkj.goldpartner.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author 杨阿旺
 * @date 2021-02-28 16:27
 */
@RestController
@RequestMapping(value = "/stock")
@CrossOrigin //跨域
@Api(value = "/api", tags = {"库存设置"})
public class StockController {
    @Autowired
    private StockService stockService;

    private Map map;

    public static String addFlag = "add";

    /**
     * 添加销售出库
     *
     * @param stock
     * @return Result
     */
    @PostMapping("addOutStock")
    @ApiOperation(value = "添加销售出库")
    public Result addOutStock(@RequestBody OutStock stock) {
        try {
            String outStock_num = stockService.getOut_num();
            Integer id = 0;
            if (outStock_num != null) {
                id = Integer.parseInt(outStock_num) + 1;
            } else {
                id = 1;
            }
            outStock_num = String.format("%08d", id);
            stock.setOut_num(outStock_num);
//            stock.setStep1_date(DateFormatHelper.currentTimes());
            stock.setOut_status("0");
            stockService.addOutStock(stock);
            List<StockInfo> infoList = stock.getStockInfos();
            StockInfo info = null;
            for (int i = 0; i < infoList.size(); i++) {
                info = infoList.get(i);
                info.setStock_num(outStock_num);
                info.setStock_status("1");
                stockService.addStockInfo(info);
            }
            info = null;
            return new Result(true, StatusCode.OK, "添加成功", "success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败", "error");
        }
    }

    /**
     * 审核复合出库
     *
     * @param outStock
     * @return Result
     */
    @PostMapping("updateOutStock")
    @ApiOperation(value = "审核复合出库")
    public Result updateOutStock(@RequestBody OutStock outStock) {
        try {
            outStock.setOut_status("1");
//            outStock.setStep2_date(DateFormatHelper.currentTimes());
            stockService.editOutStock(outStock);
            List<StockInfo> infoList = outStock.getStockInfos();
            StockInfo info = null;
            for (int i = 0; i < infoList.size(); i++) {
                info = infoList.get(i);
                stockService.addGoodsStock(info.getShop_id(), info.getGoods_id(), -info.getGoods_number(), info.getInfo_note(),
                        info.getBatch_number(), info.getProduction_date(), info.getExpiration(), info.getWarehouse_id(),
                        0.0);
            }
            info = null;
            return new Result(true, StatusCode.OK, "审核复合成功", "true");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "审核复合失败", "false");
        }
    }

    /**
     * 获取不同状态的出货单
     *
     * @param shop_id
     * @param out_status
     * @return Result
     */
    @PostMapping("getOutStock")
    @ApiOperation(value = "获取不同状态的出货单")
    public Result getOutStock(@RequestParam("shop_id") Integer shop_id, @RequestParam("out_status") String out_status) {
        try {
            List<OutStock> outStocks = stockService.getOutStock(shop_id, out_status);
            return new Result(true, StatusCode.OK, "获取成功", outStocks);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    /**
     * 获取指定的库存记录
     *
     * @param stock_status
     * @param stock_num
     * @return
     */
    @PostMapping("getStockInfo")
    @ApiOperation(value = "获取指定的库存记录")
    public Result getStockInfo(@RequestParam("stock_status") String stock_status, @RequestParam("stock_num") String stock_num) {
        try {
            List<Map> infos = stockService.getStockInfo(stock_status, stock_num);
            return new Result(true, StatusCode.OK, "获取成功", infos);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    /**
     * 获取未完成订单
     *
     * @param shop_id
     * @param supplier_id
     * @param status
     * @return
     */
    @PostMapping("getInStock")
    @ApiOperation(value = "获取未完成订单")
    public Result getInStock(@RequestParam("shop_id") Integer shop_id, Integer supplier_id, @RequestParam("status") String status) {
        try {
            if (supplier_id == null) {
                supplier_id = 0;
            }
            List<Map> inStocks = stockService.getInStock(shop_id, supplier_id, status);
            return new Result(true, StatusCode.OK, "获取成功", inStocks);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    /**
     * 添加进货单
     *
     * @param inStock
     * @return
     */
    @PostMapping("addInStock")
    @ApiOperation(value = "添加进货单")
    public Result addInStock(@RequestBody InStock inStock) {
        try {
            Double total_sum = 0.00;
            StockInfo info = null;
            List<StockInfo> infos = inStock.getStockInfos();
            for (int i = 0; i < infos.size(); i++) {
                info = infos.get(i);
                total_sum += info.getGoods_number() * info.getIn_price();
            }
            String inStock_num = stockService.getIn_num();
            Integer id = 0;
            if (inStock_num != null) {
                id = Integer.parseInt(inStock_num) + 1;
            } else {
                id = 1;
            }
            inStock_num = String.format("%08d", id);
            inStock.setIn_num(inStock_num);
            inStock.setTotal_sum(total_sum);
//            inStock.setStep1_date(DateFormatHelper.currentTimes());
            inStock.setIn_status("0");
            stockService.addInStock(inStock);
            for (int i = 0; i < infos.size(); i++) {
                info = infos.get(i);
                info.setStock_num(inStock_num);
                info.setStock_status("0");
                stockService.addStockInfo(info);
            }
            info = null;
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    /**
     * 添加确认单
     *
     * @param checkStock
     * @return
     */
    @PostMapping("addCheckStock")
    @ApiOperation(value = "添加确认单")
    public Result addCheckStock(CheckStock checkStock) {
        try {
            checkStock.setStock_status("0");
            checkStock.setStep2_date(DateFormatHelper.currentTimes());
            stockService.addCheckStock(checkStock);
            stockService.updateInStock(checkStock.getIn_num(), "1");
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    /**
     * 添加质检单
     *
     * @param qualityStock
     * @return
     */
    @PostMapping("addQualityStock")
    @ApiOperation(value = "添加质检单")
    public Result addQualityStock(QualityStock qualityStock) {
        try {
            qualityStock.setStock_status("0");
            qualityStock.setStep3_date(DateFormatHelper.currentTimes());
            stockService.addQualityStock(qualityStock);
            stockService.updateInStock(qualityStock.getIn_num(), "2");
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    /**
     * 添加复合单
     *
     * @param reviewStock
     * @return
     */
    @PostMapping("addReviewStock")
    @ApiOperation(value = "添加复合单")
    public Result addReviewStock(@RequestBody ReviewStock reviewStock) {
        try {
            reviewStock.setStock_status("0");
//            reviewStock.setStep4_date(DateFormatHelper.currentTimes());
            stockService.addReviewStock(reviewStock);
            stockService.updateInStock(reviewStock.getIn_num(), "3");
            List<StockInfo> infoList = reviewStock.getStockInfos();
            StockInfo info = null;
            for (int i = 0; i < infoList.size(); i++) {
                info = infoList.get(i);
                stockService.addGoodsStock(info.getShop_id(), info.getGoods_id(), info.getGoods_number(), info.getInfo_note(),
                        info.getBatch_number(), info.getProduction_date(), info.getExpiration(), reviewStock.getWarehouse_id(),
                        info.getIn_price());

            }
            stockService.updateStockInfoWarehouse(reviewStock.getIn_num(), reviewStock.getWarehouse_id());
            info = null;
            return new Result(true, StatusCode.OK, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }

    /**
     * 获取仓库列表
     *
     * @param shop_id
     * @param warehouse_id
     * @param in_price
     * @param category_id
     * @return
     */
    @PostMapping("getGoodsStock")
    @ApiOperation(value = "获取仓库列表")
    public Result getGoodsStock(@RequestParam("shop_id") Integer shop_id, @RequestParam("warehouse_id") Integer warehouse_id, @RequestParam("in_price") Double in_price, @RequestParam("category_id") Integer category_id) {
        try {
            List<Map> goodsStocks = stockService.getGoodsStock(shop_id, warehouse_id, in_price, category_id);
            return new Result(true, StatusCode.OK, "获取成功", goodsStocks);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "获取失败");
        }
    }

    /**
     * 仓库调拨
     *
     * @param ids
     * @param nums
     * @param exWare_id
     * @param newWare_id
     * @param wareMan_id
     * @param note
     * @return
     */
    @PostMapping("wareChange")
    @ApiOperation(value = "仓库调拨")
    public Result wareChange(@RequestParam("ids") String ids, @RequestParam("nums") String nums, @RequestParam("exWare_id") Integer exWare_id, @RequestParam("newWare_id") Integer newWare_id, @RequestParam("wareMan_id") Integer wareMan_id, String note) {
        try {
            List<GoodsStock> stockList = stockService.getGoodsStockById(ids);
            GoodsStock stock = null;
            WareChange wareChange = null;
            String[] num = nums.split(",");
            for (int i = 0; i < stockList.size(); i++) {
                stock = stockList.get(i);
                wareChange = new WareChange();
                wareChange.setExWare_id(exWare_id);
                wareChange.setWareChange_status("0");
                wareChange.setStock_id(stock.getStock_id());
                wareChange.setDate_time(DateFormatHelper.currentTimes());
                wareChange.setGoods_num(Integer.parseInt(num[i]));
                wareChange.setNewWare_id(newWare_id);
                wareChange.setWareMan_id(wareMan_id);
                wareChange.setNote(note);
                wareChange.setShop_id(stock.getShop_id());
                stock.setGoods_number(stock.getGoods_number() - Integer.parseInt(num[i]));
                stockService.editGoodsStock(stock);
                stockService.addGoodsStock(stock.getShop_id(), stock.getGoods_id(), Integer.parseInt(num[i]), "", stock.getBatch_number(), stock.getProduction_date(), stock.getExpiration(), newWare_id, stock.getIn_price());
                stockService.addWareChange(wareChange);
            }
            return new Result(true, StatusCode.OK, "调拨成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "调拨失败");
        }
    }

    /**
     * 仓库报损/报溢
     *
     * @param ids
     * @param nums
     * @param warehouse_id
     * @param wareMan_id
     * @param check_id
     * @param note
     * @param reason
     * @param flag
     * @return
     */
    @ApiOperation(value = "仓库报损/报溢")
    @PostMapping("updateWareChange")
    public Result updateWareChange(@RequestParam("ids") String ids, @RequestParam("nums") String nums, @RequestParam("warehouse_id") Integer warehouse_id, @RequestParam("wareMan_id") Integer wareMan_id, @RequestParam("check_id") Integer check_id, String note, @RequestParam("reason") String reason, @RequestParam("flag") String flag) {
        try {
            List<GoodsStock> stockList = stockService.getGoodsStockById(ids);
            GoodsStock stock = null;
            WareChange wareChange = null;
            String[] num = nums.split(",");
            for (int i = 0; i < stockList.size(); i++) {
                stock = stockList.get(i);
                wareChange = new WareChange();
                wareChange.setExWare_id(0);
                if (addFlag.equals(flag)) {
                    wareChange.setWareChange_status("2");
                } else {
                    wareChange.setWareChange_status("1");
                }
                wareChange.setStock_id(stock.getStock_id());
                wareChange.setCheck_id(check_id);
                wareChange.setDate_time(DateFormatHelper.currentTimes());
                wareChange.setGoods_num(Integer.parseInt(num[i]));
                wareChange.setNewWare_id(warehouse_id);
                wareChange.setReason(reason);
                wareChange.setWareMan_id(wareMan_id);
                wareChange.setNote(note);
                wareChange.setShop_id(stock.getShop_id());
                if (addFlag.equals(flag)) {
                    stock.setGoods_number(stock.getGoods_number() + Integer.parseInt(num[i]));
                } else {
                    stock.setGoods_number(stock.getGoods_number() - Integer.parseInt(num[i]));
                }
                stockService.editGoodsStock(stock);
                stockService.addWareChange(wareChange);
            }
            return new Result(true, StatusCode.OK, "调拨成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "调拨失败");
        }
    }

    /**
     * 商品进货明细
     *
     * @param shop_id
     * @param goods_id
     * @param start_time
     * @param end_time
     * @return
     */
    @ApiOperation(value = "货品进货明细")
    @PostMapping("/listGoodsInDetail")
    public Result listGoodsInDetail(Integer shop_id, Integer goods_id, String start_time, String end_time) {
        try {
            List<Map<String, Object>> goodsInDetail = stockService.listGoodsInDetail(shop_id, goods_id, start_time, end_time);
            return new Result(true, StatusCode.OK, "查询成功", goodsInDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", "error");
        }
    }

    @ApiOperation(value = "供货商供货明细")
    @PostMapping("/listGYGoodsDetail")
    public Result listGYGoodsDetail(Integer shop_id, Integer supplier_id, String start_time, String end_time) {
        try {
            List<Map<String, Object>> goodsDetail = stockService.listGyDetail(shop_id, supplier_id, start_time, end_time);
            return new Result(true, StatusCode.OK, "查询成功", goodsDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", "error");
        }
    }

    @ApiOperation(value = "flag--0货品销售明细,flag--1业务员销售明细，flag--2客户销售明细")
    @PostMapping("/listGoodsSaleDetail")
    public Result listGoodsSaleDetail(Integer shop_id, Integer goods_id, String start_time, String end_time, String flag) {
        try {
            List<Map<String, Object>> goodsDetail = stockService.listGoodsSaleDetail(shop_id, goods_id, start_time, end_time, flag);
            return new Result(true, StatusCode.OK, "查询成功", goodsDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", "error");
        }
    }

    @ApiOperation(value = "库存明细")
    @PostMapping("/stockDetail")
    public Result stockDetail(Integer shop_id, Integer goods_id, Integer warehouse_id, String goods_num, Integer category_id, Integer flag) {
        try {
            List<Map<String, Object>> goodsStockList = stockService.listStockDetail(shop_id, goods_id, warehouse_id, goods_num, category_id, flag);
            return new Result(true, StatusCode.OK, "查询成功", goodsStockList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", "error");
        }
    }

    @ApiOperation(value = "库存数量汇总")
    @PostMapping("/stockTotalList")
    public Result stockTotalList(Integer shop_id, Integer warehouse_id) {
        try {
            List<Map<String, Object>> stockTotalList = stockService.stockTotalList(shop_id, warehouse_id);
            return new Result(true, StatusCode.OK, "查询成功", stockTotalList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", "error");
        }
    }

    @GetMapping("/exportGoodsInDetail")
    @ApiOperation(value = "商品进货明细表导出")
    public void exportGoodsInDetail(Integer shop_id, Integer goods_id, String start_time, String end_time, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> goodsInDetail = stockService.listGoodsInDetail(shop_id, goods_id, start_time, end_time);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("商品进货明细表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("进货日期");
            row.createCell(2).setCellValue("商品名称");
            row.createCell(3).setCellValue("批号");
            row.createCell(4).setCellValue("生产日期");
            row.createCell(5).setCellValue("有效期");
            row.createCell(6).setCellValue("进货价");
            row.createCell(7).setCellValue("进货数量");
            row.createCell(8).setCellValue("总金额");
            row.createCell(9).setCellValue("UDI");
            for (Map root : goodsInDetail) {
                String total = String.valueOf(Double.valueOf(root.get("goods_number").toString()) * Double.valueOf(root.get("in_price").toString()));
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(root.get("step4_date").toString());
                row.createCell(2).setCellValue(root.get("goods_name").toString());
                row.createCell(3).setCellValue(root.get("batch_number").toString());
                row.createCell(4).setCellValue(root.get("production_date").toString());

                row.createCell(5).setCellValue(root.get("expiration").toString());
                row.createCell(6).setCellValue(root.get("in_price").toString());
                row.createCell(7).setCellValue(root.get("goods_number").toString());
                row.createCell(8).setCellValue(total);
                row.createCell(9).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("商品进货明细表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportStock")
    @ApiOperation(value = "库存明细表导出")
    public void exportStock(Integer shop_id, Integer goods_id, Integer warehouse_id, String goods_num, Integer category_id, Integer flag, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> goodsStockList = stockService.listStockDetail(shop_id, goods_id, warehouse_id, goods_num, category_id, flag);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("库存明细表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("货品代码");
            row.createCell(2).setCellValue("商品名称");
            row.createCell(3).setCellValue("所在仓库");
            row.createCell(4).setCellValue("当前库存");
            row.createCell(5).setCellValue("批号");
            row.createCell(6).setCellValue("规格型号");
            row.createCell(7).setCellValue("生产日期");
            row.createCell(8).setCellValue("有效期");
            row.createCell(9).setCellValue("养护方式");
            row.createCell(10).setCellValue("养护周期");
            row.createCell(11).setCellValue("生产企业");
            row.createCell(12).setCellValue("备注");
            row.createCell(13).setCellValue("UDI");
            for (Map root : goodsStockList) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(root.get("goods_num").toString());
                row.createCell(2).setCellValue(root.get("goods_name").toString());
                row.createCell(3).setCellValue(root.get("warehouse_name").toString());
                row.createCell(4).setCellValue(root.get("goods_number").toString());
                row.createCell(5).setCellValue(root.get("batch_number").toString());
                row.createCell(6).setCellValue(root.get("goods_format").toString());
                row.createCell(7).setCellValue(root.get("production_date").toString());
                row.createCell(8).setCellValue(root.get("expiration").toString());
                row.createCell(9).setCellValue(root.get("curing_type").toString());
                row.createCell(10).setCellValue(root.get("curing_time").toString());
                row.createCell(11).setCellValue(root.get("product_factory").toString());
                row.createCell(12).setCellValue(root.get("goods_note").toString());
                row.createCell(13).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("库存明细表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/exportGYSDetail")
    @ApiOperation(value = "供应商供货明细表导出")
    public void exportGYSDetail(Integer shop_id, Integer supplier_id, String start_time, String end_time, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> goodsDetail = stockService.listGyDetail(shop_id, supplier_id, start_time, end_time);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("供应商供货明细表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("供应商名称");
            row.createCell(2).setCellValue("时间");
            row.createCell(3).setCellValue("商品名称");
            row.createCell(4).setCellValue("批号");
            row.createCell(5).setCellValue("生产日期");
            row.createCell(6).setCellValue("有效期");
            row.createCell(7).setCellValue("进货数量");
            row.createCell(8).setCellValue("进货价");
            row.createCell(9).setCellValue("总金额");
            row.createCell(10).setCellValue("UDI");
            for (Map root : goodsDetail) {
                row = sheet.createRow(i + 1);
                String total = String.valueOf(Double.valueOf(root.get("goods_number").toString()) * Double.valueOf(root.get("in_price").toString()));
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(root.get("supplier_name").toString());
                row.createCell(2).setCellValue(root.get("step4_date").toString());
                row.createCell(3).setCellValue(root.get("goods_name").toString());
                row.createCell(4).setCellValue(root.get("batch_number").toString());
                row.createCell(5).setCellValue(root.get("production_date").toString());
                row.createCell(6).setCellValue(root.get("expiration").toString());
                row.createCell(7).setCellValue(root.get("goods_number").toString());
                row.createCell(8).setCellValue(root.get("in_price").toString());
                row.createCell(9).setCellValue(total);
                row.createCell(10).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("供应商供货明细表表.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/exportlistGoodsSaleDetail")
    @ApiOperation(value = "货品销售明细表导出")
    public void exportlistGoodsSaleDetail(Integer shop_id, Integer goods_id, String start_time, String end_time, String flag, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> goodsDetail = stockService.listGoodsSaleDetail(shop_id, goods_id, start_time, end_time, flag);
            HSSFWorkbook wb = new HSSFWorkbook();
            String num = "0";
            String num1 = "1";
            String num2 = "2";
            String table_name = num.equals(flag) ? "货品销售明细表" : (num1.equals(flag) ? "业务员销售明细表" : "客户销售明细表");
            HSSFSheet sheet = wb.createSheet(table_name);
            int i = 0;

            // "flag--0货品销售明细,flag--1业务员销售明细，flag--2客户销售明细"
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            if (num.equals(flag)) {
                row.createCell(1).setCellValue("客户名称");
            }
            if (num1.equals(flag)) {
                row.createCell(1).setCellValue("业务员名称");
            }
            if (num2.equals(flag)) {
                row.createCell(1).setCellValue("客户名称");
            }
            row.createCell(2).setCellValue("货品名称");
            row.createCell(3).setCellValue("时间");
            row.createCell(4).setCellValue("批号");
            row.createCell(5).setCellValue("生产日期");
            row.createCell(6).setCellValue("有效期");
            row.createCell(7).setCellValue("销售数量");
            row.createCell(8).setCellValue("零售价");
            row.createCell(9).setCellValue("总金额");
            row.createCell(10).setCellValue("UDI");

            for (Map root : goodsDetail) {
                row = sheet.createRow(i + 1);
                String total = String.valueOf(Double.valueOf(root.get("goods_number").toString()) * Double.valueOf(root.get("out_price").toString()));
                row.createCell(0).setCellValue(i + 1);
                if (num.equals(flag)) {
                    row.createCell(1).setCellValue(root.get("customer_name").toString());

                }
                if (num1.equals(flag)) {
                    row.createCell(1).setCellValue(root.get("member_name").toString());
                }
                if (num2.equals(flag)) {
                    row.createCell(1).setCellValue(root.get("customer_name").toString());
                }
                row.createCell(2).setCellValue(root.get("goods_name").toString());
                row.createCell(3).setCellValue(root.get("step2_date").toString());
                row.createCell(4).setCellValue(root.get("batch_number").toString());
                row.createCell(5).setCellValue(root.get("production_date").toString());
                row.createCell(6).setCellValue(root.get("expiration").toString());
                row.createCell(7).setCellValue(root.get("goods_number").toString());
                row.createCell(8).setCellValue(root.get("out_price").toString());
                row.createCell(9).setCellValue(total);
                row.createCell(10).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String((table_name + ".xls").getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 库存不足预警
     *
     * @param shop_id
     * @param warehouse_id
     * @return
     */
    @PostMapping("/listStockWarning")
    @ApiOperation(value = "库存不足预警")
    public Result listStockWarning(Integer shop_id, Integer warehouse_id) {
        try {
            List<Map<String, Object>> stockList = stockService.listStockWarning(shop_id, warehouse_id);
            return new Result(true, StatusCode.OK, "查询成功", stockList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败", "error");
        }
    }


    @GetMapping("/exportListStockWarning")
    @ApiOperation(value = "库存预警导出")
    public void exportListStockWarning(Integer shop_id, Integer warehouse_id, HttpServletResponse response) throws IOException {
        try {
            List<Map<String, Object>> stockList = stockService.listStockWarning(shop_id, warehouse_id);
            HSSFWorkbook wb = new HSSFWorkbook();
            String table_name = "库存预警表";
            HSSFSheet sheet = wb.createSheet(table_name);
            int i = 0;

            // "flag--0货品销售明细,flag--1业务员销售明细，flag--2客户销售明细"
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("仓库名称");
            row.createCell(2).setCellValue("货品代码");
            row.createCell(3).setCellValue("货品名称");
            row.createCell(4).setCellValue("型号及规格");
            row.createCell(5).setCellValue("单位");
            row.createCell(6).setCellValue("生产日期");
            row.createCell(7).setCellValue("有效期");
            row.createCell(8).setCellValue("库存数量");
            row.createCell(9).setCellValue("库存安全值");
            row.createCell(10).setCellValue("UDI");

            for (Map root : stockList) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(i + 1);
                row.createCell(1).setCellValue(root.get("warehouse_name").toString());
                row.createCell(2).setCellValue(root.get("goods_num").toString());
                row.createCell(3).setCellValue(root.get("goods_name").toString());
                row.createCell(4).setCellValue(root.get("goods_format").toString());
                row.createCell(5).setCellValue(root.get("goods_unit").toString());
                row.createCell(6).setCellValue(root.get("production_date").toString());
                row.createCell(7).setCellValue(root.get("expiration").toString());
                row.createCell(8).setCellValue(root.get("goods_number").toString());
                row.createCell(9).setCellValue(root.get("goods_warning").toString());
                row.createCell(10).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String(("库存预警表.xls").getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取到期商品库存
     *
     * @param shop_id
     * @return
     */
    @PostMapping("/getGoodsStockWarn")
    @ApiOperation(value = "获取到期商品库存")
    public Result getGoodsStockWarn(@RequestParam("shop_id") Integer shop_id) {
        try {
            List<Map> goodsStocks = stockService.getGoodsStockWarn(shop_id);
            return new Result(true, StatusCode.OK, "查询成功", goodsStocks);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    @GetMapping("/exportGoodsStockWarn")
    @ApiOperation(value = "货品有效期预警表")
    public void exportGoodsStockWarn(Integer shop_id, HttpServletResponse response) throws IOException {
        try {
            List<Map> goodsStocks = stockService.getGoodsStockWarn(shop_id);
            HSSFWorkbook wb = new HSSFWorkbook();
// 根据页面index 获取sheet页
            HSSFSheet sheet = wb.createSheet("货品有效期预警表");
            int i = 0;
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("货品代码");
            row.createCell(1).setCellValue("货品名称");
            row.createCell(2).setCellValue("规格型号");
            row.createCell(3).setCellValue("单位");
            row.createCell(4).setCellValue("注册证号/备案凭证号");
            row.createCell(5).setCellValue("批号/灭菌批号/序号");
            row.createCell(6).setCellValue("生产日期");
            row.createCell(7).setCellValue("有效期(月)");
            row.createCell(8).setCellValue("库存数量");
            row.createCell(9).setCellValue("生产企业");
            row.createCell(10).setCellValue("UDI");
            for (Map root : goodsStocks) {
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(root.get("goods_num").toString());
                row.createCell(1).setCellValue(root.get("goods_name").toString());
                row.createCell(2).setCellValue(root.get("goods_format").toString());
                row.createCell(3).setCellValue(root.get("goods_unit").toString());
                row.createCell(4).setCellValue(root.get("registration_NO").toString());
                row.createCell(5).setCellValue(root.get("batch_number").toString());
                row.createCell(6).setCellValue(root.get("production_date").toString());
                row.createCell(7).setCellValue(root.get("expiration").toString());
                row.createCell(8).setCellValue(root.get("goods_number").toString());
                row.createCell(9).setCellValue(root.get("product_factory").toString());
                row.createCell(10).setCellValue(root.get("UDI").toString());
                i++;
            }
// 输出Excel文件
            OutputStream output = response.getOutputStream();
            response.reset();
// 设置文件头
            response.setHeader("Content-Disposition",
                    "attchement;filename=" + new String("货品有效期预警表s.xls".getBytes("gb2312"), "ISO8859-1"));
            response.setContentType("application/msexcel");
            wb.write(output);
            wb.close();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
